const db = require('../db/connMysql');

const harmDao = {
    /*
     * 获取所有危机类型
     */
    async getAllHarmType() {
        let result = await new Promise((resolve, reject) => {
            db.getConnection((err, connection) => {
                if (err) {
                    // 连不上数据库
                    connection.release();
                    reject(err);
                    return ;
                }
                let sql = `select * from CrisisTypes`;
                connection.query(sql, [], function (err, result) {
                    if (err) {
                        // 连上数据库之后，执行sql时发生错误
                        connection.release();
                        reject(err);
                        return ;
                    }
                    connection.release();
                    resolve(result);
                });
            });
        });
        return result;
    },
    /*
     * 添加危机记录
     */
    async addHarmRecord(data) {
        let result = await new Promise((resolve, reject) => {
            db.getConnection((err, connection) => {
                if (err) {
                    // 连不上数据库
                    connection.release();
                    reject(err);
                    return ;
                }
                let insertData = [
                    data.studentId,
                    data.time,
                    data.address,
                    data.crisisTypeId,
                    data.note
                ];
                let sql = `insert into CrisisRecords (
                            studentId, time, address, crisisTypeId, note
                        ) values (?, ?, ?, ?, ?)`;
                connection.query(sql, insertData, function (err, result) {
                    if (err) {
                        // 连上数据库之后，执行sql时发生错误
                        connection.release();
                        reject(err);
                        return ;
                    }
                    connection.release();
                    resolve(result);
                });
            });
        });
        return result;
    },
    /*
     * 查询某个学生的所有危机档案（暂时不做细节权限限制）
     */
    async getHarmRecordByStudent(uid, studentId) {
        let result = await new Promise((resolve, reject) => {
            db.getConnection((err, connection) => {
                if (err) {
                    // 连不上数据库
                    connection.release();
                    reject(err);
                    return ;
                }
                let sql = `select * from CrisisRecords where studentId = ?`;
                connection.query(sql, [studentId], function (err, result) {
                    if (err) {
                        // 连上数据库之后，执行sql时发生错误
                        connection.release();
                        reject(err);
                        return ;
                    }
                    connection.release();
                    resolve(result);
                });
            });
        });
        return result;
    },
    /*
     * 获取所有预警级别的名称
     */
    async getAllWarningLevels() {
        let result = await new Promise((resolve, reject) => {
            db.getConnection((err, connection) => {
                if (err) {
                    // 连不上数据库
                    connection.release();
                    reject(err);
                    return ;
                }
                let sql = `select name from WarningLevels`;
                let data = [];
                connection.query(sql, data, function (err, result) {
                    if (err) {
                        // 连上数据库之后，执行sql时发生错误
                        connection.release();
                        console.log(err);
                        reject(err);
                        return ;
                    }
                    connection.release();
                    resolve(result);
                });
            });
        });

        return result;
    },
    /*
     * 获取所有中心意见
     */
    async getAllCenterOpinions() {
        let result = await new Promise((resolve, reject) => {
            db.getConnection((err, connection) => {
                if (err) {
                    // 连不上数据库
                    connection.release();
                    reject(err);
                    return ;
                }
                let sql = `select centerOpinion as name from CenterOpinion`;
                let data = [];
                connection.query(sql, data, function (err, result) {
                    if (err) {
                        // 连上数据库之后，执行sql时发生错误
                        connection.release();
                        console.log(err);
                        reject(err);
                        return ;
                    }
                    connection.release();
                    resolve(result);
                });
            });
        });

        return result;
    }
};

module.exports = harmDao;
